<?php

class db {

    private $conn = null;
    private $database = null;

    public function __construct($database = MYSQL_APPLICATION_DB, $user = MYSQL_UID, $pass = MYSQL_PWD, $server = MYSQL_HOST, $port = MYSQL_PORT) {
        $conn = new mysqli($server, $user, $pass, $database, $port);
        if (!is_object($conn) || is_null($conn->host_info)) {
            throw new Exception("Kan ikke etablere forbindelse til database.");
        } else {

            if ($conn->set_charset("utf8")) {
                $this->conn = $conn;
                $this->database = $database;
                return true;
            } else {
                throw new Exception("Kan ikke charset (utf8) på databasen.");
            }
        }
    }

    public function __destruct() {
        $this->conn->close();
        unset($this->conn);
    }

    public function getError() {
        return $this->conn->error;
    }

    public function get($select = "*", $table = "", $where = "", $orderby = "", $limit = 0, $showsql = false) {
        return $this->_get($select, $table, $where, $orderby, $limit, false, $showsql);
    }

    public function getFirst($select = "*", $table = "", $where = "", $orderby = "", $showsql = false) {
        return $this->_get($select, $table, $where, $orderby, 1, true, $showsql);
    }

    private function _get($select = "*", $table = "", $where = "", $orderby = "", $limit = 0, $first = false, $showsql = false) {

        $sql = "SELECT $select ";

        if (strstr($table, "`")) {
            $sql = $sql . "FROM $table ";
        } else {
            $sql = $sql . "FROM `$table` ";
        }

        if ($where != "") {
            $sql = $sql . "WHERE ($where) ";
        }

        if ($orderby != "") {
            $sql = $sql . "ORDER BY $orderby ";
        }

        if ($limit > 0 || stristr($limit, ",")) {
            $sql = $sql . "LIMIT $limit";
        }

        if ($showsql) {
            echo $sql;
        }


        $result = $this->conn->query($sql);

        if (!$result) {
            return false;
        }

        if (!$first) {
            $array = array();
            While ($row = $result->fetch_array(MYSQLI_ASSOC)) {

                $row = $this->decode($row);
                $array[] = $row;
            }
            if (is_array($array)) {
                return $array;
            } else {
                return false;
            }
        } else {
            $row = $result->fetch_array(MYSQLI_ASSOC);
            $row = $this->decode($row);
            return $row;
        }
    }

    public function sqlexec($sql) {
        return $this->conn->query($sql);
    }

    private function decode($array) {

        if (is_array($array)) {

            $keys = array_keys($array);

            foreach ($keys as $k) {
                if (!is_numeric($array[$k])) {
                    //$array[$k] = htmlspecialchars_decode($array[$k],ENT_QUOTES);
                    $array[$k] = stripcslashes($array[$k]);
                }
            }

            return $array;
        } else {
            return false;
        }
    }

    public function getColumns($table = "") {

        $sql = "SHOW COLUMNS ";

        if (strstr($table, "`")) {
            $sql = $sql . "FROM $table ";
        } else {
            $sql = $sql . "FROM `$table` ";
        }


        $result = $this->conn->query($sql);

        if (!$result) {
            return false;
        }


        While ($row = $result->fetch_array(MYSQLI_ASSOC)) {

            $row = $this->decode($row);
            $array[] = $row;
        }
        return $array;
    }

    public function getCount($where, $table) {

        $sql = "SELECT COUNT(*) AS num ";
        if (strstr($table, "`")) {
            $sql = $sql . "FROM $table ";
        } else {
            $sql = $sql . "FROM `$table` ";
        }
        if ($where != "") {
            $sql = $sql . "WHERE ($where)";
        }

        $result = $this->conn->query($sql);
        if ($result == false) {
            return 0;
        } else {
            $row = $result->fetch_array(MYSQLI_ASSOC);
            return $row['num'];
        }
    }

    public function getTablestatus($table) {
        $result = $this->conn->query("SHOW TABLE STATUS FROM " . $this->database . " WHERE `Name`='$table'");

        $row = $result->fetch_array(MYSQLI_ASSOC);
        $row = $this->decode($row);
        return $row;
    }
    
    public function escapestring($string) {
        return $this->conn->real_escape_string($string);
    }
    
}
